<template>
  <div class="user-conversion">
    <div class="main-hd">
      <h2>模型配置</h2>
    </div>
    <div class="main-bd">
      <div class="content-wrap">
        <Table border :columns="utColumns" :data="utList" :loading="utLoading"></Table>
      </div>
    </div>

    <Modal
      v-model="billStatDetailModal"
      title="编辑"
      :footer-hide="true"
      width="500">
      <div>
        <Form ref="repayForm" :model="repayForm" :label-width="100">
          <FormItem label="模型名称">
            <Input type="text" placeholder="模型名称" v-model="repayForm.name"></Input>
          </FormItem>
          <FormItem label="贷前权重">
            <Input type="text" placeholder="贷前权重" v-model="repayForm.preLoanWeight"></Input>
          </FormItem>
          <FormItem label="贷前规则摘要">
            <Input type="text" placeholder="贷前规则摘要" v-model="repayForm.preLoanDesc"></Input>
          </FormItem>
          <FormItem label="贷中权重">
            <Input type="text" placeholder="贷中权重" v-model="repayForm.inLoanWeight"></Input>
          </FormItem>
          <FormItem label="贷中规则摘要">
            <Input type="text" placeholder="贷中规则摘要" v-model="repayForm.inLoanDesc"></Input>
          </FormItem>
          <FormItem label="状态">
            <RadioGroup v-model="repayForm.status">
              <Radio label="1">启用</Radio>
              <Radio label="0">禁用</Radio>
            </RadioGroup>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleSubmit">
              <span>确定</span>
            </Button>
          </FormItem>
        </Form>
      </div>
    </Modal>

    <Modal
			v-model="pfVisible"
      title="导流渠道列表"
			:mask-closable="false"
			footer-hide>
			<div class="modal-inner">
        <Table
          ref="platformTable"
          border
          :columns="pfColumns"
          :data="pfData"
          :loading="pfLoading"></Table>
      </div>
		</Modal>
  </div>  
</template>

<script>
  import qs from 'qs';

  export default {
    name: 'modelConfig',
    data () {
      return {
        billStatDetailModal: false,
        switchStatus: false,
        repayForm: {},
        utList: [],
        utColumns: [
          { title: '模型名称', key: 'name', align: 'center' },
          { title: '模型编号', key: 'rmNo', align: 'center' },
          { title: '贷前权重', key: 'preLoanWeight', align: 'center'},
          {
            title: '贷前规则摘要',
            key: 'preLoanDesc',
            align: 'center'
          },
          { 
            title: '贷中权重',
            key: 'inLoanWeight',
            align: 'center'
          },
          { 
            title: '贷中规则摘要',
            key: 'inLoanDesc',
            align: 'center'
          },
          { 
            title: '更新时间',
            key: 'utime',
            align: 'center',
            render: (h, params) => {
              var d = new Date(params.row.utime);
              return h('div', [ [ d.getFullYear(), d.getMonth() + 1, d.getDate(), '-'], [ d.getHours(), d.getMinutes(), d.getSeconds(), ':'] ]
              .map(arr => {
                var split = arr.pop();
                return arr.map(val => val > 9 ? val : '0' + val).join(split);
              }).join(' '))
            }
          },
          { 
            title: '状态',
            key: 'status',
            align: 'center',
            render: (h, params) => {
              return h('div', params.row.status == 1 ? '启用' : '禁用')
            }
          },
          { 
            title: '操作',
            align: 'center',
            render: (h, params) => {
              const { rmNo } = params.row;
							return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.editRiskConfig(params.row);
                    }
                  }
                }, '编辑'),
                h('Button', {
                  style: {
                    marginLeft: '5px'
                  },
                  props: {
                    type: 'success',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.checkDetail(rmNo);
                    }
                  }
                }, '查看')
							]);
					  }
          }
        ],
        utLoading: true,
        pageSize: 10,
        page: 1,
        total: 0,
        pfVisible: false,
        pfLoading: true,
        pfData: [],
        pfColumns: [
          { title: '导流渠道ID', key: 'platformId', align: 'center' },
          { title: '导流渠道名称', key: 'platformName', align: 'center' }
        ]
      }
    },
    created () {
      this.getModelConfig();
    },
    methods: {
      // 模型配置
      getModelConfig () {
        this.$http.get('/helsinki/admin/risk-model')
        .then((data) => {
          if (data.body.errcode == 0) {
            this.utList = data.body.data;
          }
          this.utLoading = false;
        }, error => {});
      },
      editRiskConfig (params) {
        this.repayForm.rmNo = params.rmNo;
        this.repayForm.name = params.name;
        this.repayForm.preLoanWeight = params.preLoanWeight;
        this.repayForm.inLoanWeight = params.inLoanWeight;
        this.repayForm.status = params.status;
        this.repayForm.preLoanDesc = params.preLoanDesc;
        this.repayForm.inLoanDesc = params.inLoanDesc;
        this.repayForm.status == 1 ? this.repayForm.status = '1' : this.repayForm.status = '0';
        this.billStatDetailModal = true;
      },
      handleSubmit (){
        let {name,preLoanWeight,inLoanWeight,status,preLoanDesc,inLoanDesc} = this.repayForm;
        this.$http.put('/helsinki/admin/risk-model?rmNo='+this.repayForm.rmNo+'&name='+name+'&preLoanWeight='+preLoanWeight+'&inLoanWeight='+inLoanWeight+'&status='+status+'&preLoanDesc='+preLoanDesc+'&inLoanDesc='+inLoanDesc)
        .then((data) => {
          if (data.body.errcode == 0) {
            this.getModelConfig();
            this.$Message.info('修改成功');
          } else {
            this.getModelConfig();
          }
          this.billStatDetailModal = false;
        }, error => {
          this.getModelConfig();
        });
      },
      // 获取指定风控模型绑定的导流渠道
      checkDetail(rmNo) {
        this.$http.get(`/helsinki/admin/risk-model/bonded-platform?rmNo=${rmNo}`)
        .then(({ data: { data, info, errcode } }) => {
          if (errcode == 0) {
            this.pfData = data.list;
            this.pfLoading = false;
            this.pfVisible = true;
          }
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  .search-con {
    margin-bottom: 18px;
    .search-col {
      margin-left: 12px;
    }
    .search-col,
    .search-input {
      display: inline-block;
      width: 200px;
    }
  }

  .table-con {
    margin-top: 30px;
    .table-title {
      margin-bottom: 18px;
    }
    .expand-more {
      line-height: 32px;
      border-left: 1px solid #e8eaec;
      border-right: 1px solid #e8eaec;
      border-bottom: 1px solid #e8eaec;
      font-size: 12px;
      text-align: center;
      cursor: pointer;
    }
  }

  .ivu-page {
    margin-top: 12px;
    text-align: right;
  }
</style>
